<template>
	<view class="imgCont" style="border: none;">
		<view class="main">
			<!-- 一级分类 -->
			<view class="active_tit paddingNone">
				<view class="imgTit">
					<slot></slot> 
				</view>
				<view style="display: flex;">
					<view class="imgBtn" @click="upTree">
						更新担保人影像目录
					</view>
					<!-- <view class="imgBtn" @click="isShow">
						{{showText}}
					</view> -->

				</view>

			</view>
			<!-- 二级分类 -->
			<block >
				<block v-for="item in treeData">
					<view class="active_tit paddingNone">
						<view class="imgTit2">
							{{item.label}}
						</view>
						<view class="imgBtn" @click="isShowItem(item)">
							{{item.showText}}
						</view>
					</view>
					<block v-if="item.show">
						<block v-for="eitem in item.children">
							<view class="active_tit paddingNone">
								<view class="imgTit3">
									{{eitem.label}}
								</view>
								<view class="imgBtn" @click="isShowItem(eitem)">
									{{eitem.showText}}
								</view>
							</view>

							<block v-if="eitem.show">
								<block v-for="zitem in eitem.children">
									<view class="active_tit paddingNone">
										<view class="imgTit3">
											{{zitem.label}}
										</view>
									</view>
									<block v-for="imgItem in imageData[zitem.code]">
										<view class="choose-img-box"
											v-if="imgItem.base64!=''&&imgItem.businessTag==zitem.name">
											<icon v-if="imgItem.base64!=''" class="icon" type="cancel" size="26"
												@click="remove(imageData[zitem.code],imgItem,zitem.code)" />
											<image class="img" :src="imgItem.img" @click="lookImg(imgItem.img)" />
										</view>

									</block>
									<view class="choose-img-box">
										<image class="img" src="/static/enterprise_card.png"
											@click="chooseImg(imageData[zitem.code],zitem.code,zitem.name)" />
									</view>
								</block>
							</block>
						</block>


					</block>


				</block>
			</block>

		</view>

	</view>

</template>

<script>
	import chooseImgToBase64 from "@/utils/chooseImgToBase64";
	export default {
		data() {
			return {
				showText: '展开',
				show: false,
				treeData: [{
						label: '①个人类保证人身份证明材料',
						showText: '展开',
						children: [

						]

					},
					{
						label: '②公司类保证人基本材料',
						showText: '展开',
						children: [

						]

					},
				],

				imageData: {
					SDMS_0301_01: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0301_02: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0301_03: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0302_09: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0302_26: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0302_11: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0401_29: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0401_30: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0401_27: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0402_11: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0403_25: [{
						img: '/static/enterprise_card.png',
						base64: ''
					}],
					SDMS_0301_06:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0301_07:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0301_08:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0301_27:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0303_27:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0303_18:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0303_08:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0304_28:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0401_06:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0401_07:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0401_08:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0403_18:[{img:'/static/enterprise_card.png',base64:''}],
				}
			}
		},
		props: {
			changeFlag: {
				type: Number
			},
		},
		watch: {
			changeFlag: {
				handler: function(res) {
					//console.warn('数据变化',res)
					this.imageData = getApp().globalData.imageData.dbztData

				},
				deep: true
			},
			imageData: {
				handler: function(res) {
					//console.warn('数据变化',res)
					getApp().globalData.imageData.dbztData = res
				},
				deep: true
			}
		},
		mounted() {
			this.fileQuery('SDMS_0301')
			this.fileQuery('SDMS_0302')
			this.fileQuery('SDMS_0303')
			this.fileQuery('SDMS_0304')
			this.fileQuery('SDMS_0401')
			this.fileQuery('SDMS_0402')
			this.fileQuery('SDMS_0403')
			this.upTree()



		},
		methods: {
			fileQuery(code) {

				let obj = {
					typeCode: code,
					businessId: getApp().globalData.stdOffsurBaiscInfo.businessId,
				}
				this.sendRequest({
					url: 'ext/findImageList', //'offsur/fileQuery',
					method: 'post',
					hideLoading: true,
					data: obj,
					complete: (res) => {
						let list = Object.keys(this.imageData)
						let imgData = []
						if (res.code == '200') {
							res.data.map((item, key) => {
								item.img = 'data:image/png;base64,' + item.fileBase64
								item.base64 = item.fileBase64
							})

							//console.log(res.data)

							for (let i = 0; i < res.data.length; i++) {
								for (let j = 0; j < list.length; j++) {
									if (res.data[i].typeCode == list[j]) {
										this.imageData[list[j]].push(res.data[i])
									}
								}
							}

						}
					}
				})
			},
			chooseImg(name, code, userName) {

				chooseImgToBase64(
					(base64, base64SourceData, file) => {
						console.log(base64SourceData)
						let imgData = {
							img: base64,
							base64: base64SourceData
						}

						//this.idCardOcr("FRONT");
						this.fileUpload(code, file.name, base64SourceData, imgData, name, userName)

					}, 2048

				);
			},
			fileUpload(code, fileName, base64, item, name, userName) {
				let obj = {
					businessTag: userName,
					fileBase64: base64,
					businessId: getApp().globalData.stdOffsurBaiscInfo.businessId,
					typeCode: code,
					fileName: fileName,
					currentUserId: getApp().globalData.stdOffsurBaiscInfo.customerId
				}
				this.sendRequest({
					url: 'ext/upload',
					method: 'post',
					hideLoading: false,
					data: obj,
					complete: (res) => {

						if (res.code == '200') {
							item.fileId = res.data.fileId
							item.businessTag = userName

							name.push(item)
						} else {
							uni.showToast({
								icon: 'none',
								title: "图片上传失败，请重试",
								duration: 2000
							})
						}
					}
				})
			},
			remove(name, item) {
				let obj = {
					businessId:getApp().globalData.stdOffsurBaiscInfo.businessId,
					fileId: item.fileId,
					currentUserId: getApp().globalData.stdOffsurBaiscInfo.customerId
				}
				this.sendRequest({
					url: 'ext/deleteImage',
					method: 'post',
					hideLoading: true,
					data: obj,
					complete: (res) => {

						if (res.code == '200') {
							name.remove(item)
						}
					}
				})

			},
			isShow() {
				if (this.show) {
					this.show = false
					this.showText = '展开'
				} else {
					this.show = true
					this.showText = '收起'
				}
			},
			isShowItem(item) {
				if (item.show) {
					item.show = false
					item.showText = '展开'
				} else {
					item.show = true
					item.showText = '收起'
				}
			},
			subSet(arr1, arr2) {
				let newArr1 = []
				let newArr2 = []
				for(let i=0;i<arr1.length;i++){
					newArr1.push('担保人：' + arr1[i].idCode)
				}
				for(let j=0;j<arr2.length;j++){
					newArr2.push(arr2[j].label)
				}
				
				let set1 = new Set(newArr1);
				let set2 = new Set(newArr2);

				let subset = [];

				for (let item of set2) {
					if (!set1.has(item)) {
						subset.push(item);
					}
				}
				let arrItem = []
				for(let m=0;m<subset.length;m++){
					for(let n=0;n<arr2.length;n++){
						if(subset[m] == arr2[n].label){
							arrItem.push(arr2[n])
						}
						
					}
				}

				return arrItem;
			},
			//通过担保人数据,动态生成目录
			upTree() {
				////获取担保主体信息
				let arr = getApp().globalData.stdOffsurSceneLoanRelation
				for (let i = 0; i < arr.length; i++) {
					if (arr[i].idCode == '') {
						uni.showToast({
							icon: 'none',
							title: '请输入担保人/企业证件号！',
							duration: 2500
						})
						return false
					}
					if (arr[i].subjectType == '1') {
						let item = {
							type:'1',
							label: '担保人：' + arr[i].idCode,
							showText: '展开',
							children: [{
									name: arr[i].idCode,
									label: '身份证',
									code: 'SDMS_0301_01'
								},
								{
									name: arr[i].idCode,
									label: '婚姻状况证明材料',
									code: 'SDMS_0301_02'
								},
								{
									name: arr[i].idCode,
									label: '户口簿',
									code: 'SDMS_0301_03'
								},
								{
									name: arr[i].idCode,
									label: '征信授权书',
									code: 'SDMS_0301_06'
								},
								{
									name: arr[i].idCode,
									label: '个人类保证人征信报告',
									code: 'SDMS_0301_07'
								},
								{
									name: arr[i].idCode,
									label: '个人类保证人司法查询结果',
									code: 'SDMS_0301_08'
								},
								{
									name: arr[i].idCode,
									label: '保证人声明',
									code: 'SDMS_0301_27'
								},
								{
									name: arr[i].idCode,
									label: '房产证',
									code: 'SDMS_0302_09'
								},
								{
									name: arr[i].idCode,
									label: '行驶证',
									code: 'SDMS_0302_26'
								},
								{
									name: arr[i].idCode,
									label: '其他资产证明材料',
									code: 'SDMS_0302_11'
								},
								{
									name: arr[i].idCode,
									label: '个人类保证人经营性材料等',
									code: 'SDMS_0303_27'
								},
								{
									name: arr[i].idCode,
									label: '工商查询结果',
									code: 'SDMS_0303_18'
								},
								{
									name: arr[i].idCode,
									label: '企业司法查询结果',
									code: 'SDMS_0303_08'
								},
								{
									name: arr[i].idCode,
									label: '个人类保证人提供的其它材料',
									code: 'SDMS_0304_28'
								},
							]
						}
						let flag = this.treeData[0].children.find(item => item.label == '担保人：' + arr[i].idCode)
						if (!flag) {
							this.treeData[0].children.push(item)
							//console.log(arr[i].idCode, 'none')
							arr[i].isHave = '1'
						} else {
							//console.log(arr[i].idCode, 'have')
							arr[i].isHave = '1'
						}

					} else if (arr[i].subjectType == '2') {
						let item = {
							type:'2',
							label: '担保人：' + arr[i].idCode,
							showText: '展开',
							children: [{
									name: arr[i].idCode,
									label: '保证人身份证明',
									code: 'SDMS_0401_29'
								},
								{
									name: arr[i].idCode,
									label: '征信授权书',
									code: 'SDMS_0401_06'
								},
								{
									name: arr[i].idCode,
									label: '保证人征信报告',
									code: 'SDMS_0401_07'
								},
								{
									name: arr[i].idCode,
									label: '股东会决议',
									code: 'SDMS_0401_30'
								},
								{
									name: arr[i].idCode,
									label: '保证人声明',
									code: 'SDMS_0401_27'
								},
								{
									name: arr[i].idCode,
									label: '保证人司法查询结果',
									code: 'SDMS_0401_08'
								},
								{
									name: arr[i].idCode,
									label: '保证人资产证明',
									code: 'SDMS_0402_11'
								},
								{
									name: arr[i].idCode,
									label: '保证人经营性材料',
									code: 'SDMS_0403_25'
								},
								{
									name: arr[i].idCode,
									label: '工商查询结果',
									code: 'SDMS_0403_18'
								}

							]
						}

						let flag = this.treeData[1].children.find(item => item.label == '担保人：' + arr[i].idCode)
						if (!flag) {
							this.treeData[1].children.push(item)
							arr[i].isHave = '1'
						} else {
							arr[i].isHave = '1'
						}
					}
				}


				//console.log(this.subSet(arr, this.treeData[0].children))
				//担保人数据更新时，更新树目录
				let arrAll = [...this.treeData[0].children,...this.treeData[1].children]
				let delArr = this.subSet(arr,arrAll)
				for(let m=0;m<delArr.length;m++){
					if(delArr[m].type=='1'){
						this.treeData[0].children.remove(delArr[m])
					}else{
						this.treeData[1].children.remove(delArr[m])
					}
				}
				
			}
		}
	}
</script>

<style>

</style>
